package com.example.flshtarvel.controller;

import com.example.flshtarvel.entity.Article;
import com.example.flshtarvel.entity.Route;
import com.example.flshtarvel.entity.Tag;
import com.example.flshtarvel.service.ArticleService;
import com.example.flshtarvel.service.RouteService;
import com.example.flshtarvel.service.TagService;
import com.example.flshtarvel.vo.RouteVo;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {
    @Autowired
    TagService tagService;
    @Autowired
    ArticleService articleService;
    @Autowired
    RouteService routeService;

    //首页搜索文章，搜索框结果
    @GetMapping("/inpSearch/article")
    public Map<String,Object> inpArticleSearch(@RequestParam("search") String search){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Tag> tagList = tagService.selectTagByName(search);
        List<Article> articleList = articleService.selectAllArticleByName(search);
        if(tagList.isEmpty()&&articleList.isEmpty()){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("tagList",tagList);
            map.put("articleList",articleList);
        }
        return map;
    }
    //首页搜索路线，搜索框结果
    @GetMapping("/inpSearch/route")
    public Map<String,Object> inpRouteSearch(@RequestParam("search") String search){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Tag> tagList = tagService.selectTagByName(search);
        List<RouteVo> routeVoList = routeService.selectRouteByName(search);
        List<Article> articleList = articleService.selectAllArticleByName(search);
        if(tagList.isEmpty()&&routeVoList.isEmpty()&&articleList.isEmpty()){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("tagList",tagList);
            map.put("articleList",articleList);
            map.put("routeList",routeVoList);
        }
        return map;
    }


}
